package com.futureCloudMOM.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.futureCloudMOM.common.exception.base.BaseException;
import com.futureCloudMOM.core.base.service.impl.BaseServiceImpl;
import com.futureCloudMOM.core.domain.ProduceDispatch;
import com.futureCloudMOM.core.domain.ProduceTaskBill;
import com.futureCloudMOM.core.domain.ProduceTaskBillRoutingProcess;
import com.futureCloudMOM.core.domain.ProduceTaskBillSub;
import com.futureCloudMOM.core.mapper.ProduceDispatchMapper;
import com.futureCloudMOM.core.mapper.ProduceTaskBillSubMapper;
import com.futureCloudMOM.core.service.ProduceTaskBillRoutingProcessService;
import com.futureCloudMOM.core.service.ProduceTaskBillSubService;
import com.futureCloudMOM.core.utils.UUIDUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 【Produce_TaskBillSub】的数据库操作Service实现
 *
 * @author Administrator
 * @since 2023-06-06 16:03:28
 */
@Service
@AllArgsConstructor
public class ProduceTaskBillSubServiceImpl extends BaseServiceImpl<ProduceTaskBillSubMapper, ProduceTaskBillSub> implements ProduceTaskBillSubService {

    // 生产订单工艺工序
    private final ProduceTaskBillRoutingProcessService produceTaskBillRoutingProcessService;
    // 派工单
    private final ProduceDispatchMapper produceDispatchMapper;

    @Override
    public void create(ProduceTaskBill produceTaskBill) {
        Long billId = produceTaskBill.getId();
        List<ProduceTaskBillRoutingProcess> processes = produceTaskBillRoutingProcessService.selectListByBillId(billId);
        if (CollectionUtils.isEmpty(processes)) {
           throw new BaseException("未查询到该订单下的工艺工序信息");
        }
        processes.forEach(produceTaskBillRoutingProcess -> {
            Long taskBillRoutingProcessId = produceTaskBillRoutingProcess.getId();
            ProduceTaskBillSub taskBillSub = ProduceTaskBillSub.getInstance(produceTaskBillRoutingProcess);
            taskBillSub.setTaskBillId(billId);
            taskBillSub.setProcId(taskBillRoutingProcessId);
            setProduceTaskInfo(produceTaskBill, taskBillSub);
        });
    }

    @Override
    public void deleteByBillId(Long billId) {
        remove(new LambdaQueryWrapper<ProduceTaskBillSub>()
                .eq(ProduceTaskBillSub::getTaskBillId, billId));
    }

    @Override
    @Transactional
    public void dispatch(ProduceDispatch dto) {
        // 查询要下派的生产工单最新数据
        ProduceTaskBillSub produceTaskBillSub = getById(dto.getTaskBillSubId());
        if (produceTaskBillSub.getStatus() != 0){
            throw new BaseException("生产工单的状态为待派才可以进行下派操作");
        }
        // 该生产工单剩余的可派工数量 = 任务数量 - 已派工数量
        BigDecimal qty = produceTaskBillSub.getQty().subtract(produceTaskBillSub.getDispatchQty());
        // 此次派工数量大于0 并且小于或等于可派工数量
        if (produceTaskBillSub.getDispatchQty().compareTo(BigDecimal.ZERO) > 0 && qty.compareTo(produceTaskBillSub.getDispatchQty()) >= 0){
            // 生成派工单
            // 派工单号
            dto.setDispatchNo(UUIDUtils.createdUUID("PGD"));
            //总工时
            dto.setTotalTime(dto.getDispatchQty().multiply(dto.getStdTime()));
            //总工价
            dto.setTotalPrice(dto.getDispatchQty().multiply(dto.getStdPrice()));
            //ProduceDispatch produceDispatch = ProduceDispatch.builder()
            //        .taskBillId(dto.getTaskBillId())
            //        .taskBillSubID(dto.getId())
            //        .dispatchNo(UUIDUtils.createdUUID("PGD"))
            //        .procId(dto.getProcId())
            //        .subNo(dto.getSubNo())
            //        .departmentId(dto.getDepartmentId())
            //        .workcenterId(dto.getWorkCenterId())
            //        .resourceId(dto.getResourceId())
            //        .materialId(dto.getMaterialId())
            //        .moldId(dto.getMoldId())
            //        .moldQty(dto.getMoldQty())
            //        .dispatchDate(new Date())
            //        .planStart(dto.getPlanStart())
            //        .planEnd(dto.getPlanEnd())
            //        .qty(dto.getQty())
            //        .dispatchQty(dto.getCurDispatchQty())
            //        .unitId(dto.getProduceUnit())
            //        .priceOne(dto.getProducePriceOne())
            //        .routeType(dto.getRouteType())
            //        .notReportQty(dto.getCurDispatchQty())
            //        .stdTime(dto.getProduceCycle())
            //        .totalTime(dto.getCurDispatchQty().multiply(dto.getProduceCycle()))
            //        .totalPrice(dto.getCurDispatchQty().multiply(dto.getPriceOne()))
            //        .build();
            produceDispatchMapper.insert(dto);
            // 更新生产工单信息
            produceTaskBillSub.setDispatchQty(produceTaskBillSub.getDispatchQty().add(dto.getDispatchQty()));
            // 已派数量大于或等于任务数量 工单状态改为已派工
            if(produceTaskBillSub.getDispatchQty().compareTo(produceTaskBillSub.getQty()) >= 0){
                produceTaskBillSub.setStatus(1L);
            }
            updateById(produceTaskBillSub);
        }else{
            throw new BaseException("请核对派工数量是否超出该订单剩余可派工数量");
        }
    }

    /**
     * 填充生产订单信息
     *
     * @param taskBill    生产订单
     * @param taskBillSub 生产订单子表
     */
    private void setProduceTaskInfo(ProduceTaskBill taskBill, ProduceTaskBillSub taskBillSub) {
//        taskBillSub.setPlanEnd(taskBill.getPlanStart());
//        taskBillSub.setPlanEnd(taskBill.getPlanEnd());
        taskBillSub.setBatchNum(taskBill.getBatchNum());
    }

}
